//
//  PhotoPickerCell.m
//  标题按钮
//
//  Created by 李贤立 on 16/5/22.
//  Copyright © 2016年 李贤立. All rights reserved.
//

#import "PhotoPickerCell.h"
#import <Masonry.h>
@implementation PhotoPickerCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    [self.contentView addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.contentView addSubview:self.addBtn];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.contentView addSubview:self.deleteBtn];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.imageView);
        make.height.with.mas_equalTo(30);
    }];
}
- (void)setImage:(UIImage *)image {
    _image = image;
    self.imageView.hidden = NO;
    self.imageView.image = image;
    self.addBtn.hidden = YES;
    self.deleteBtn.hidden = NO;
}
#pragma mark - 懒加载
- (UIButton *)addBtn {
    if (_addBtn == nil) {
        _addBtn = [[UIButton alloc] init];
        [_addBtn setImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(pickImages) forControlEvents:UIControlEventTouchUpInside];
        _addBtn.hidden = YES;
    }
    return _addBtn;
}
- (UIButton *)deleteBtn {
    if (_deleteBtn == nil) {
        _deleteBtn = [[UIButton alloc] init];
        [_deleteBtn setImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteImages) forControlEvents:UIControlEventTouchUpInside];
        _deleteBtn.hidden = YES;
    }
    return _deleteBtn;
}
- (UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}
#pragma mark - UI事件
- (void)pickImages {
    // 进入选择照片控制器
    if (self.pickerImagesBlock) {
        self.pickerImagesBlock();
    }
}
- (void)deleteImages {
    if (self.deleteImagesBlock) {
        self.deleteImagesBlock();
    }
}
- (void)showAddBtn {
    self.addBtn.hidden = NO;
    self.deleteBtn.hidden = YES;
    self.imageView.hidden = YES;
}
@end
